package com.crm.mongo.service.impl;

import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import com.crm.dao.ConsultInfoManageMapper;
import com.crm.model.entity.CountWorkBench;
import com.crm.mongo.service.MongoMinService;
import com.mongodb.BasicDBObject;
import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.DBObject;
import com.mongodb.QueryOperators;

@Service("mongoMinService")
public  class MongoMinServiceImpl implements MongoMinService{

	@Autowired
	private ConsultInfoManageMapper consultInfoManageMapper;
	
	@Autowired
    protected MongoTemplate mongoTemplate;
	
	@Override
	public void save(CountWorkBench t) {
		mongoTemplate.save(t);
	}
	
	@Override
	public void saveBatch(List<CountWorkBench> list) {
		for(CountWorkBench e:list){
			mongoTemplate.save(e);
		}
	}

	@Override
	public List<CountWorkBench> task(CountWorkBench record) {
		// TODO Auto-generated method stub
		return consultInfoManageMapper.task(record);
	}

	@Override
	public List<DBObject> getByWhere(CountWorkBench record) {
		
		Pattern pattern = Pattern.compile("^.*"+record.getBeginTime()+".*$", Pattern.CASE_INSENSITIVE);
		
		  
		
	    BasicDBObject query1 = new BasicDBObject();
	    
	    query1.put("createDate", BasicDBObjectBuilder.start("$gte", new Date(2016-1900 , 12 - 1, 1)).add("$lt", new Date(2016 - 1900, 12-1 , 3)).get());
	   
	    BasicDBObject query3 = new BasicDBObject();
	    query3.put("userId",record.getUserId());
	    
		BasicDBObject queryObject = new BasicDBObject().append(  
                QueryOperators.AND,  
                new BasicDBObject[] {
                		query1,query3});  
		return mongoTemplate.getCollection("countWorkBench").find(queryObject).toArray();
	}

}
